#include <string>
#include <iostream>
#include <sstream>
#include "config.h"
#include "mem.h"
#include "subsystem.h"

using namespace std;

char static_heap_buffer[BUFFER_SIZE];
HeapAllocator* static_heap = 0;

class myclass
{
public:
    myclass(const char* str){a = 3; b = 2;};
    U32 a;
    U16 b;
};

root::root()
{
}

root::~root()
{
}

Error root::startup(const string& cfgFilename)
{
    string msg;

    // register version number
    stringstream versionMajorStrStream;
    versionMajorStrStream<<VER_MAJOR;
    stringstream versionMinorStrStream;
    versionMinorStrStream<<VER_MINOR;
    m_version = versionMajorStrStream.str() + "." + versionMinorStrStream.str();

    // register config file
    #ifdef _DEBUG
    cout<<"config system starts up"<<"\n";
    #endif
    m_cfgManager.startup(cfgFilename);

    m_pStaticHeap = new(static_heap_buffer) HeapAllocator(NULL, static_heap_buffer + sizeof(HeapAllocator), BUFFER_SIZE - sizeof(HeapAllocator));
    myclass* te = m_pStaticHeap->make_new<myclass>("test");
    ASSERT(te->a == 3);
    ASSERT(te->b == 2);
    myclass* tf = m_pStaticHeap->make_new<myclass>("test");
    ASSERT(tf->a == 3);
    ASSERT(tf->b == 2);
    m_pStaticHeap->make_delete<myclass>(te);
    PoolAllocator* pool = m_pStaticHeap->make_new<PoolAllocator>("pool_allocator", 600, 100);
    char* pCharArray1 = (char*)pool->allocate(50, 4);
    char* pCharArray2 = (char*)pool->allocate(170, 4);
    char* pCharArray3 = (char*)pool->allocate(260, 4);
    pool->deallocate(pCharArray2);
    char* pCharArray4 = (char*)pool->allocate(50, 4);
    char* pCharArray5 = (char*)pool->allocate(50, 4);
    pool->deallocate(pCharArray5);
    pool->deallocate(pCharArray1);
    pool->deallocate(pCharArray4);

    char* pCharArray6 = (char*)pool->allocate(270, 4);
    
    
    

    return kSuccess;
}

string root::getVersion()
{
    return m_version;
}
